<template>
  <a-card :bordered="false">

    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="4" :sm="8">
            <a-form-item label="经销商编号">
              <a-input placeholder="请输入经销商编号" v-model="queryParam.storeId"></a-input>
            </a-form-item>
          </a-col>
          <a-col  :md="4" :sm="8">
            <a-form-item label="经销商名称">
              <a-input placeholder="请输入经销商名称" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="8">
            <a-form-item label="销售人员">
              <a-input placeholder="请输入销售人员" v-model="queryParam.saleRealName"></a-input>
            </a-form-item>
          </a-col>
          <a-col  :md="4" :sm="8">
            <a-form-item
              label="渠道">
              <a-select
                v-decorator="[ 'channelId', {}]"
                v-model="queryParam.channelId"
                style="width: 100%"
                :getPopupContainer="node => node.parentNode"
                placeholder="请选择渠道">
                <a-select-option v-for="(channel,channelIdIndex) in optChannelList" :key="channel.id.toString()"
                                 :value="channel.id">
                  {{ channel.name }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{
          selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="storeId"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange,type:type}"
        @change="handleTableChange">

        <span slot="action" slot-scope="text, record">
          <a v-has="'store:creditFinanceApprove'" :hidden="[0].indexOf(record.creditStatus) === -1" @click="handleApprove(record)">财务审核</a>
        </span>


        <span slot='creditEnableStatus' slot-scope='text, record'>
          <a-switch size='small' :checked="(text === '1')" @change='handleCreditEnableStatus(record)'/>
        </span>
      </a-table>
    </div>

    <!-- 表单区域 -->
    <storeCreditApproveModal ref="StoreCreditApproveModal" @ok="modalFormOk" />
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import {httpAction, getAction, postAction} from '@/api/manage'
import StoreCreditApproveModal from './modules/StoreCreditApproveModal'

export default {
  name: 'StmDealerCreditLimitApprove',
  mixins: [JeecgListMixin],
  components: {
    StoreCreditApproveModal
  },
  data() {
    return {
      description: '经销商列表页面',
      channelList: [],
      optChannelList: [],
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: 'ERP编号',
          align: 'center',
          dataIndex: 'id'
        },
        {
          title: '经销商编号',
          align: 'center',
          dataIndex: 'storeId'
        },
        {
          title: '经销商名称',
          align: 'center',
          width: 120,
          dataIndex: 'name'
        },
        {
          title: '渠道',
          align: 'center',
          width: 120,
          dataIndex: 'channelName'
        },
        {
          title: '状态',
          align: 'center',
          dataIndex: 'status_dictText'
        },
        {
          title: '授信开关',
          align: 'center',
          dataIndex: 'creditEnableStatus',
          scopedSlots: {customRender: 'creditEnableStatus'}
        },
        {
          title: '销售人员',
          align: 'center',
          dataIndex: 'saleRealName'
        },
        {
          title: '授信状态',
          align: 'center',
          dataIndex: 'creditStatus_dictText'
        },
        {
          title: '授信金额(元)',
          align: 'center',
          dataIndex: 'creditLimit'
        },
        {
          title: '授信余额(元)',
          align: 'center',
          dataIndex: 'remainCreditLimit'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      type: "radio",
      url: {
        list: '/stm/stmStoreInfo/creditList',
        channelListUrl: 'stm/stmChannelInfo/list',
        updateCreditEnableStatus: 'stm/stmStoreInfo/updateCreditEnableStatus',
      }
    }
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  created() {
    this.getChannelList();
    this.queryParam.storeType = '1',
    this.queryParam.brandCategory = 2
  },
  methods: {
    getChannelProduct(id){
      let param={
        storeId: id,
        pageNo: 1,
        pageSize: 10
      }
      getAction(this.url.storeChannelList, param).then((res) => {
        if (res.success) {
          this.channelList=res.result.records;
          if(this.channelList.length>0){
            this.$refs.StmChannelProductList.getMain(this.channelList[0].channelId);
          }
          else{
            this.$refs.StmChannelProductList.getMain(-1);
          }
        } else {
          this.$message.warning(res.message);
        }
      });
    },
    onClearSelected() {
      this.selectedRowKeys = [];
      this.selectionRows = [];
    },

    searchQuery:function(){
      this.selectedRowKeys = [];
      this.selectionRows = [];
      this.loadData(1);
    },
    getChannelList() {
      let param={};
      param.status=1;
      param.pageSize=100;
      getAction(this.url.channelListUrl, param).then(res => {
        if (res.success) {
          this.optChannelList = res.result.records;
          this.optChannelList.push({id:"",name:"请选择渠道"})
          this.optChannelList.reverse();
        } else {
          console.log(res.message)
        }
      })
    },
    handleApprove(record){
      this.$refs.StoreCreditApproveModal.title = "授信审核";
      this.$refs.StoreCreditApproveModal.show(record);
      this.$refs.StoreCreditApproveModal.disableSubmit = false;
    },
    handleCreditEnableStatus(record) {
      if (record.creditEnableStatus == '1') {
        record.creditEnableStatus = '0'
      } else {
        record.creditEnableStatus = '1'
      }
      let editObj = {}
      editObj.id = record.id
      editObj.creditEnableStatus = record.creditEnableStatus
      const that = this
      that.confirmLoading = true
      postAction(this.url.updateCreditEnableStatus, editObj).then((res) => {
        if (res.success) {
          that.$message.success(res.message)
          that.$emit('ok')
        } else {
          that.$message.warning(res.message)
          if (record.publishStatus == '1') {
            record.publishStatus = '0'
          } else {
            record.publishStatus = '1'
          }
          if (record.storeStatus == '1') {
            record.storeStatus = '0'
          } else {
            record.storeStatus = '1'
          }
        }
      }).finally(() => {
        that.confirmLoading = false;
      })
    },
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
